package com.service;

import com.alibaba.fastjson.JSON;
import com.entity.QiShou_db;
import com.utils.EntityFactory;
import com.utils.ExecUtil;
import com.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/qishou_add", "/qishou_update", "/qishou_query"})
public class QiShouService_db extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getServletPath().equals("/qishou_query")) {
            resp.getWriter().write(Result.fail(404, "网址错误"));
            return;
        }
        boolean judge = true;
        String sql = "select * from qishou_db";
        List<QiShou_db> qiShous = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        if (!req.getParameter("name").isEmpty()) {
            params.add(req.getParameter("name"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " name like \"%\"?\"%\"";
        }
        if (!req.getParameter("phone").isEmpty()) {
            params.add(req.getParameter("phone"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " phone like \"%\"?\"%\"";
        }
        if (!req.getParameter("time1").isEmpty()) {
            params.add(req.getParameter("time1"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " time >= ?";
        }
        if (!req.getParameter("time2").isEmpty()) {
            params.add(req.getParameter("time2"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " time <= ?";
        }
        try {
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory(QiShou_db.class);
                QiShou_db qiShou = (QiShou_db) entityFactory.startCombine(resultSet);
                qiShous.add(qiShou);
            }
            resp.getWriter().write(JSON.toJSONString(qiShous));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sql;
        List<Object> params = new ArrayList<>();
        String data;
        if (!req.getServletPath().equals("/qishou_add") && !req.getServletPath().equals("/qishou_update")) {
            resp.getWriter().write(Result.fail(404, "网址错误"));
            return;
        }
        if (req.getServletPath().equals("/qishou_add")) {
            sql = "insert into qishou_db(name,phone,time) values(?,?,?)";
            params.add(req.getParameter("name"));
            params.add(req.getParameter("phone"));
            params.add(new Date());
            data = "添加成功";
        } else {
            sql = "update qishou_db set name=?,phone=? where id = ?";
            params.add(req.getParameter("name"));
            params.add(req.getParameter("phone"));
            params.add(req.getParameter("id"));
            data = "编辑成功";
        }
        try {
            ExecUtil.execute(sql, params, req, true);
            resp.getWriter().write(Result.success(data));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
